package com.netstar.util;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.view.WindowManager;

public class ContextUtils {

	private static float sRate = -1.0f;

	public static float getSizeRate(Context context) {
		if (sRate > 0) {
			return sRate;
		}
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics metrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(metrics);
		sRate = metrics.density / 1.5f;
		return sRate;
	}

	/**
	 * Return type of the current network, return -1 if currently there is no
	 * network.
	 */
	public static int getNetworkType(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		if (null == ni || !ni.isAvailable()) {
			return -1;
		}
		return ni.getType();
	}

	public static void showInstalledAppDetails(Activity context, String packageName) {
		Intent intent = new Intent();
		final int apiLevel = Build.VERSION.SDK_INT;
		if (apiLevel >= 9) {
			intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
			Uri uri = Uri.fromParts("package", packageName, null);
			intent.setData(uri);
		} else {
			final String appPkgName = (apiLevel == 8 ? "pkg" : "com.android.settings.ApplicationPkgName");
			intent.setAction(Intent.ACTION_VIEW);
			intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
			intent.putExtra(appPkgName, packageName);
		}
		try {
			context.startActivity(intent);
		} catch (ActivityNotFoundException e) {
		}
	}

	public static String getDeviceID(Context cxt) {
		TelephonyManager tm = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceID = tm.getDeviceId();
		return (null != deviceID && deviceID.length() > 0) ? deviceID : "unknown";
	}

	public static String getTelephoneNumber(Context cxt) {
		TelephonyManager tm = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
		String number = tm.getLine1Number();
		return (null != number && number.length() > 0) ? number : "unknown";
	}

	/**
	 * Return mobile network code for the device.
	 * 
	 * <li>1 ---China Mobile
	 * 
	 * <li>2 ---China Unicom
	 * 
	 * <li>3 ---China Telecom
	 * 
	 * <li>-1 --Unknown
	 */
	public static int getMncIdentifier(Context cxt) {
		TelephonyManager tm = (TelephonyManager) cxt.getSystemService(Context.TELEPHONY_SERVICE);
		String operator = tm.getSimOperator();
		if (null == operator) {
			return 0;
		}
		if (operator.equals("46000") || operator.equals("46002")) {
			return 1;
		} else if (operator.equals("46001")) {
			return 2;
		} else if (operator.equals("46003")) {
			return 3;
		}
		return -1;
	}

	public static String getMncDescription(int mncId) {
		String s = "无服务";
		switch (mncId) {
			case 1:
				s = "中国移动";
				break;
			case 2:
				s = "中国联通";
				break;
			case 3:
				s = "中国电信";
		}
		return s;
	}

}
